package cn.com.aiouyi.entity;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import lombok.Getter;
import lombok.Setter;


@Getter
@Setter
@Entity
@Table(name="t_article")
public class Article extends SeqEntity<Long>{
	
	private static final long serialVersionUID = 1L;

	/** @Fields 文章标题*/
	@NotEmpty(message = "标题不能为空")
	private String title;
	
	/** @Fields 文章作者*/
	@NotEmpty(message = "作者不能为空")
	private String author;

	/** @Fields 文章分类*/
	@ManyToMany
	@JoinTable(name = "article_articleclass", joinColumns = { @javax.persistence.JoinColumn(name = "article_id") }, inverseJoinColumns = { @javax.persistence.JoinColumn(name = "articleClass_id") })
	private List<ArticleClass> articleClasses = new ArrayList();

	@Transient
	private ArticleClass default_ac;
	/** 此文章涉及多个文章分类时，默认获取第一个 */
	@Transient
	public ArticleClass getDefaultAc() {System.out.println("size:"+this.articleClasses.size());
		if(this.articleClasses.size()>0){
			return this.articleClasses.get(0);
		}
		return null;
	}

	/** @Fields 此文章有多个文章分类时   拼接出分类名字*/
	@Transient
	public String getArticleClassIds(){
		String ac_ids = "";
		for(ArticleClass ac : this.articleClasses){
			ac_ids += ac.getId()+",";
		}
		if(!ac_ids.equals("")){
			ac_ids = ac_ids.substring(0, ac_ids.length()-1); // 去掉最后一个逗号
		}
		return ac_ids;
	}
	
	/** @Fields 此文章有多个文章分类时   拼接出分类名字*/
	@Transient
	public String getArticleClassNames(){
		String ac_names = "";
		for(ArticleClass ac : this.articleClasses){
			ac_names += ac.getName()+",";
		}
		if(!ac_names.equals("")){
			ac_names = ac_names.substring(0, ac_names.length()-1); // 去掉最后一个逗号
		}
		return ac_names;
	}
	
	/** @Fields 文章宣传主图*/
	@OneToOne
	private Accessory mainPic;
	
	/** @Fields 文章html内容*/
	@Lob
	@Column(columnDefinition="LongText")
	private String contentHtml;
	
	/** @Fields 文章文本内容*/
	@Lob
	@Column(columnDefinition="LongText")
	private String contentText;

	/** @Fields 是否在前台显示*/
	@Column(columnDefinition="bit default true")
	@NotNull(message = "是否展示不能为空")
	private Boolean display;
	
	/** @Fields 是否置顶*/
	@Column(columnDefinition="bit default false")
	private Boolean top;
	
	/** @Fields 文章的状态 (-1:删除， 0:发布，  1：草稿)*/
	@Column(columnDefinition="int default 0")
	private Integer status;
	
	/** @Fields 阅读量*/
	@Column(columnDefinition="int default 0")
	private Integer readAmount;


    public Boolean containsClass(ArticleClass articleClass){
        if (this.articleClasses.contains(articleClass)){
            return true;
        }
        return false;
    }
}
